#include <iostream>
#include <vector>
using namespace std;

class ObjectPool{
  const int C;
  int head;
  vector<int> next;
  vector<bool> used;
public:
  ObjectPool(int capacity);
  void allocate();
  void use(int p);
  void release(int p);
};

ObjectPool::ObjectPool(int capacity) : C(capacity), head(1), next(C + 1), used(C + 1) {
  for(int i = 1; i < C; ++i){
    next[i] = i + 1;
    used[i] = false;
  }
  next[C] = 0;
  cout << "capacity " << C << " # head=" << head << "\n";
}

void ObjectPool::allocate(){
  int p = head;
  // TODO: 
  cout << "new # return " << p << ", head=" << head << "\n";
}

void ObjectPool::use(int p){
  if(false){ // TODO: 检查p是否越界、是否有效
    cout << "ref " << p << " # error\n";
  }else{
    cout << "ref " << p << " # ok\n";
  }
}

void ObjectPool::release(int p){
  if(false){ // TODO: 检测p有效性
    cout << "delete " << p << " # error\n";
  }else{
    if(p){
      // TODO: 调整head和next，归还p
    }
    cout << "delete " << p << " # head=" << head << "\n";
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string cmd;
  int capacity;
  cin >> cmd >> capacity;
  ObjectPool pool(capacity);
  for(int p; cin >> cmd;){
    if(cmd == "new"){
      pool.allocate(); // 处理分配请求
    }else if(cmd == "ref"){
      cin >> p;
      pool.use(p); // 处理使用请求
    }else if(cmd == "delete"){
      cin >> p;
      pool.release(p); // 处理释放请求
    }
  }
}
